Skip to content

Neo4j: isNull and isNotNull on an association test the relationship - #16836

Open
borinquenkid wants to merge 15 commits into
8.0.xfrom
fix/neo4j-isnull-association-8.0.x
Open

borinquenkid wants to merge 15 commits into
8.0.xfrom
fix/neo4j-isnull-association-8.0.x

Conversation

@borinquenkid

@borinquenkid borinquenkid commented Nov 3, 2026 •

Copy link
Copy Markdown
Member

Fixes #16584
Fixes #14512

An association is stored as a relationship, so n.parent IS NULL is true for every node and isNull('parent') matched all of them. On an association, isNull now compiles to NOT (n)-[:PARENT]->(:Folder) and isNotNull to the pattern itself. Plain properties and relationship entities are unchanged.

  • IsNullAssociationSpec runs the reproduction from the issue plus isNotNull, a combination with a property criterion and the unchanged plain-property case. Three of its four features fail without the change.
  • The whole Neo4j core suite passes.

Once this merges, the @PendingFeatureIf Neo4j marker on UniqueConstraintNullGroupValueSpec in #16583 can be removed.

https://claude.ai/code/session_01Dmki4SQGwRSj8pRJ5KCWyX

An association is stored as a relationship, so n.parent IS NULL is true for
every node. isNull now becomes NOT (n)-[:PARENT]->(:Folder) and isNotNull the
pattern itself. Plain properties and relationship entities are unchanged.

Fixes #16584

Claude-Session: https://claude.ai/code/session_01Dmki4SQGwRSj8pRJ5KCWyX
Copilot AI balanced review requested due to automatic review settings November 3, 2026 03:48

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copilot was unable to review this pull request because the user who requested the review has insufficient remaining quota. Try requesting a review at a lower effort level.

@jdaugherty jdaugherty left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

For a criterion on the root entity the fix does what it says. isNull / isNotNull return the right rows on a to-one, a to-many, the inverse side of a bidirectional association and an association to a relationship entity, and so do findAllBy<Association>IsNull, findAllBy<Association>(null), where { association == null }, not { }, or { } and deleteAll(). Without the main change three of the four features of IsNullAssociationSpec fail, and with it the Neo4j core suite passes.

What has to change before this goes in:

  1. Criteria nested in an association resolve the property on the wrong entity. One query that was right on 8.0.x now returns wrong rows, and the nested form of #16584 is not fixed.
  2. The change in results needs an upgrade note.
  3. The relationship entity guard and most branches of the new pattern have no test.

#16583 carries its own copy of this change for 9.0.x, written inline in both handlers, with an Embedded exclusion and a RelationshipMappingSpec feature that this PR does not have. Both edit the same lines, so the merge up will conflict unless the copy in #16583 is dropped once this one is merged.

CypherExpression handle(GraphPersistentEntity entity, Query.IsNull criterion, CypherBuilder builder, String prefix) {
String relationship = associationRelationshipPattern(entity, criterion.property, prefix)
if (relationship != null) {
return new CypherExpression("NOT ${relationship}")

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This changes the rows existing queries return, so it needs a section in the Grails 8 upgrade guide (grails-doc/src/en/guide/upgrading/upgrading80x.adoc), also when the earlier result was a bug. For GORM for Neo4j the note has to cover:

  • isNull on an association returned every instance and now returns only those without the relationship; isNotNull returned none and now returns those with it.
  • The same holds for findAllBy<Association>IsNull / IsNotNull, findAllBy<Association>(null), the countBy forms, and where { association == null } / != null.
  • On a to-many association, isNull now means the collection has no element.
  • deleteAll() and updateAll() follow the query: Pet.where { owner == null }.deleteAll() deleted every pet on 8.0.x and now deletes only the pets without an owner.

It should also say how to get the earlier rows back, which is to remove the criterion.

CypherExpression handle(GraphPersistentEntity entity, Query.IsNotNull criterion, CypherBuilder builder, String prefix) {
String relationship = associationRelationshipPattern(entity, criterion.property, prefix)
if (relationship != null) {
return new CypherExpression("${relationship}")

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nit: new CypherExpression(relationship). The GString adds nothing.

Comment on lines +671 to +673
if (entity.isRelationshipEntity()) {
return null
}

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No test reaches this branch: the Neo4j core suite passes with these three lines removed. Without them CastMember.createCriteria().list { isNull('from') } fails with Type mismatch: expected Node but was Relationship, because the pattern starts at the relationship variable (NOT (r)-[]->(:Movie)). Please add a feature that runs isNull and isNotNull on from and to of a relationship entity, for example in RelationshipMappingSpec.

if (entity.isRelationshipEntity()) {
return null
}
PersistentProperty property = entity.getPropertyByName(propertyName)

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

entity is not always the entity the criterion belongs to. AssociationQueryHandler dispatches the criteria nested in an association with the entity it was given and only switches the prefix, so inside owner { ... } this lookup runs against the class the query started from. With Pet { Owner owner; Kind kind } and Owner { String kind; Address address }:

  • Pet.createCriteria().list { owner { isNull('kind') } } returned the pets whose owner has no kind on 8.0.x (m_0.kind IS NULL). Here kind resolves to the association Pet.kind, the condition becomes NOT (m_0)-[:KIND]->(:Kind), and every pet with an owner comes back; isNotNull('kind') returns nothing. A null test on a plain property that worked is now wrong whenever the root class has an association of the same name.
  • owner { isNull('address') } and Pet.where { owner.address == null } still render m_0.address IS NULL and return every pet with an owner, because Pet has no address. That is Neo4j: isNull on an association matches every node #16584 one level down, and this change does not reach it.

One way to fix both: in AssociationQueryHandler, dispatch the nested criteria with association.associatedEntity instead of entity, keeping entity when the association targets a relationship entity. With that, both queries return the right rows and the Neo4j core suite still passes. Please add a feature for each of the two cases to IsNullAssociationSpec.

return null
}
PersistentProperty property = entity.getPropertyByName(propertyName)
if (property instanceof Association && !(property instanceof Basic)) {

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Not blocking: Embedded is an Association and not a Basic, so a null test on an embedded property now renders NOT (n)-[:HOME]->(:Address). An embedded component is flattened onto the owner's node and never has a relationship, so by the javadoc above this method should return null for it. The rows are the same as before (all for isNull, none for isNotNull), so nothing regresses, but please add Embedded to the exclusion.

* the relationship and not for a property of the node.
*/
@Issue('16584')
class IsNullAssociationSpec extends Neo4jGormDatastoreSpec {

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The spec exercises one shape, a unidirectional to-one to the same class. The null test now goes through formatAssociationPatternFromExisting, and these branches have no feature:

  • the reversed pattern of a bidirectional many-to-one ((n)<-[:ITEMS]-(:Shelf)),
  • a to-many,
  • an association to a relationship entity ((n)<-[]-(:Celeb) for Movie.cast),
  • the Basic exclusion in associationRelationshipPattern.

The first three return the right rows when I run them, so this is missing coverage and not a defect. Please add features for them.

@jamesfredley jamesfredley left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Not approved. A nested isNull resolves the property on the root, so a scalar on the joined entity is tested as the root's relationship. An embedded component is treated as a relationship and the query throws.

…entity

A criterion nested in an association block was dispatched with the entity the query
started from, so isNull inside owner { } looked the property up on the wrong class:
a plain property of the same name as an association of the root was tested as a
relationship, and an association of the owner, which the root does not have, was
tested as a property. The nested criteria are now dispatched with the associated
entity, as for an association to a relationship entity they keep the entity given.

An embedded component is stored on the node and stays a property null test; it was
rewritten to a relationship pattern and failed in the cast of its entity.

Tests for the nested cases, the reversed pattern of a bidirectional many-to-one, a
to-many, a basic collection, an embedded component, an association to a relationship
entity and the ends of a relationship entity. The upgrade guide gets a note.

Claude-Session: https://claude.ai/code/session_01Dmki4Tgxp7dLBhzpe2xT93
@borinquenkid

Copy link
Copy Markdown
Member Author

Pushed 5f50268. By thread:

Nested criteria: AssociationQueryHandler dispatches the criteria of an association block with the associated entity, and keeps the entity it was given when the association targets a relationship entity. owner { isNull('kind') } now tests the owner's plain property, and owner { isNull('address') } tests the owner's association. Features for both, with isNotNull, and one for an association of the root entity beside a nested block. This is also james's comment on the property resolved on the root.

Upgrade note: section 90 of the Grails 8 upgrade guide, with isNull and isNotNull, the finder and where forms, to-many, nested blocks, deleteAll and updateAll, and how to get the earlier rows back.

Embedded: the relationship pattern excludes Embedded as well as Basic. Without the exclusion an embedded component's null test fails in the cast of its entity, as james found; a feature runs isNull and isNotNull on one.

Tests: the reversed pattern of a bidirectional many-to-one, a to-many, a basic collection, an association to a relationship entity (Movie.cast) and isNull and isNotNull on from and to of CastMember. I mutated the nested dispatch, the Embedded exclusion and the relationship entity guard, and a feature fails for each. The GString nit is done.

I did not add the where { owner.address == null } form: in the spec the where closure did not resolve the nested path, so the nested block form carries both cases.

On #16583: its copy of this change is for 9.0.x; it should be dropped when this one merges up.

@jamesfredley jamesfredley left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Not approved. A null test inside a relationship-entity block treats a scalar as the owning node's association, and Neo4j rejects the query. A nested isNull on a node association now uses the joined entity. An embedded component is no longer treated as a relationship. Threads PRRT_kwDOAAfRJ860EUgW and PRRT_kwDOAAfRJ860EUgp resolved.

…perties

A null test inside a block on an association to a relationship entity looked
the property up on the node the block is on, so a relationship property with
the same name as an association of the node built a relationship pattern from
a relationship variable, which Neo4j rejects. The builder now records the
variable of a relationship, and the null test there stays a property test.

Claude-Session: https://claude.ai/code/session_01Dmki4Tgxp7dLBhzpe2xT93
@borinquenkid

Copy link
Copy Markdown
Member Author

@jamesfredley Fixed in 4271678, on top of 5f50268.

connections { isNull('spouse') } now tests the relationship's own property. I reproduced your case first: a node with a spouse association and a relationship entity with a plain spouse property made Neo4j reject NOT (m_0)-[]->(...), because the pattern started at the relationship variable. The query builder now records the variable of a relationship, and a null test on that variable is a property test.

IsNullAssociationSpec has the feature (NullTestMate with a spouse association, NullTestBond with a spouse string): isNull returns the end of the bond with no spouse, and isNotNull the other one. The Neo4j core suite and codeStyle pass.

Not changed here: eq('spouse', ...) in the same block still looks the property up on the node, and it did so before this PR too. It is a separate fix, so I left it out.

@jamesfredley jamesfredley left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Not approved. A nested comparison uses the block entity's id mapping, so a query that used to match returns no rows. A null test on a relationship entity's scalar now tests that property. Thread PRRT_kwDOAAfRJ860U6b- resolved.

if (criterion.entity instanceof RelationshipPersistentEntity) {
builder.addRelationshipVariable(nextPrefix)
}
return dispatchCriterion(aq.criteria, nested, builder, nextPrefix)

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

customer { eq('account', account) } now compares the customer's id column. Customer uses a snowflake id and Account uses a native id, so the comparison looks for id and matches no order. It used to compare the account's id. Use the target of account for that comparison.

…entity

A comparison with an association, eq or ne on a property that leads to another node, formatted the
identity of the node it reached with the identity mapping of the entity that owns the association.
With a snowflake id on the owner and a native id on the associated entity the comparison looked for
the wrong identity and matched nothing, inside an association block and on the entity itself.

Claude-Session: https://claude.ai/code/session_01Dmki4RzWwreEC2GkkyFnvL
@borinquenkid

Copy link
Copy Markdown
Member Author

@jamesfredley Fixed in 1da133a, on top of 4271678. You were right, and it was wider than the block: eq and the other comparisons (ne, gt, ...) on an association formatted the identity of the node they reach with the id mapping of the entity that owns the association. With a snowflake id on Customer and a native id on Account, the comparison looked for __id__ on an Account node and matched nothing, inside customer { eq('account', account) } and on Customer itself. Both handlers now use the identity of the associated entity.

NestedAssociationIdentitySpec has the three cases (eq in the block, ne in the block, eq on the entity itself) with a snowflake Customer, a native Account and an Order. All three fail without the change. The Neo4j core suite and codeStyle pass.

@jamesfredley jamesfredley left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

A nested comparison on the associated entity now uses that entity's identity mapping. Not approving. A relationship criterion inside the block still uses the owner's mapping, so a query that used to match returns no rows.

// recorded as a relationship: the properties are those of the relationship, which has no
// relationship of its own to test.
PersistentEntity associated = association.associatedEntity
GraphPersistentEntity nested = associated instanceof GraphPersistentEntity &&

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Input: a native-id root, a snowflake-id owner, and idEq on a native-id relationship inside that owner block. The relationship criterion uses the owner's identity mapping, so a query that used to match returns no rows. Use the relationship's own mapping for identity and comparison criteria, not only for null tests.

…the relationship entity

A block on an association to a relationship entity declares a variable for the relationship and now
records the relationship entity with it. A criterion on that variable takes the identity and the plain
properties from that entity, so idEq and an equality on the identity compare ID(relationship) when the
relationship has a native id and its owner a snowflake one, and not the owner's identifier property.
An association of the relationship, and a property it has no mapping for, stay with the block's entity.

Claude-Session: https://claude.ai/code/session_01Dmki4RzWwreEC2GkkyFnvL
@borinquenkid

Copy link
Copy Markdown
Member Author

@jamesfredley fixed in 0ad7f66. A block on an association to a relationship entity now records the relationship entity with its variable, and a criterion on that variable takes its identity and plain properties from that entity. For your input (a native-id root, a snowflake-id owner, idEq on a native-id relationship in the owner block) the query compares ID(m_1) and not the owner's m_1.__id__. eq('id', ...) on the relationship does the same. An association of the relationship, and a property it has no mapping for, stay with the block's entity.

RelationshipBlockIdentitySpec has three features: idEq, eq('id', ...), and a plain property. The two identity ones fail without the change. The Neo4j core suite passes.

@jamesfredley jamesfredley left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Not approving. A scalar property of a relationship, such as total, is no longer qualified, so the query throws.

# Conflicts:
#	grails-doc/src/en/guide/upgrading/upgrading80x.adoc
…ariable

Only the identity of a relationship in a block uses the relationship entity; other properties stay with the entity of the block so that total compares as m_0.total.

@jamesfredley jamesfredley left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Not approving. A scalar property of a relationship is still unqualified, so the query throws. A null check on a relationship also matches a different relationship type.

if (entity.isRelationshipEntity() || builder.isRelationshipVariable(prefix)) {
return null
}
PersistentProperty property = entity.getPropertyByName(propertyName)

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

A movie connected to a celebrity only by a different relationship type satisfies isNotNull('cast') and fails isNull('cast'). The pattern does not name the relationship type. Include that type.

…mes the relationship type

The pattern of an association to a relationship entity had no type, so a node
connected to another by a relationship of a different type counted as having the
association: isNotNull matched it and isNull did not.

Claude-Session: https://claude.ai/code/session_01Dmki4RzWwreEC2GkkyFnvL
…rionScope

Seventeen criterion handlers each took the entity, the builder and the variable
and worked out for themselves where a property is: Equals and the comparison
handler each carried a copy of the association, relationship end and type
resolution, and Between and the property comparison wrote the variable in front
of the name by hand, so they ignored the identity and relationship rules.

CriterionScope holds the entity, the variable and the query, and answers what a
property refers to, what a null test on an association looks like, and which
scope the block of an association opens. A handler asks it and only says what it
compares.

Claude-Session: https://claude.ai/code/session_01Dmki4RzWwreEC2GkkyFnvL
@borinquenkid

Copy link
Copy Markdown
Member Author

@jamesfredley pushed ff5c7e3. Your last review's two findings:

  • A scalar property of a relationship (links { eq('total', 1L) }) is qualified with the relationship variable again. This was in 6e861ac, with a spec.
  • A null test on an association to a relationship entity now names the relationship type, so a node linked by a different type no longer counts as having the association (c5bf41a, spec in IsNullAssociationSpec).

ff5c7e3 moves the decision about what a property refers to into one class, CriterionScope, instead of each criterion handler deciding it. Between and the property comparison now go through it as well, with a spec for a relationship block. Neo4j core: 648 tests, 0 failures.

@jamesfredley jamesfredley left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Not approving. A disjunction that also tests membership of an association drops the rows that match the other branch. A range on a relationship property whose name starts with to or from is still unqualified.

int paramNumber = addBuildParameterForCriterion(builder, entity, criterion)
GraphPersistentEntity graphPersistentEntity = (GraphPersistentEntity) entity
String lhs
String lhs = scope.reference(criterion.property)

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

or { eq('name', 'three'); inList('owner', [a.id]) }, with pet three having no owner and pet one belonging to a. The list returns only one. three is dropped before the name test can keep it. Do not require the association for the whole query.

int paramNumberFrom = builder.addParam(mappingContext.convertToNative(criterion.from))
int parmaNumberTo = builder.addParam(mappingContext.convertToNative(criterion.to))
new CypherExpression("\$$paramNumberFrom<=${prefix}.$criterion.property and ${prefix}.$criterion.property<=\$$parmaNumberTo")
String property = scope.property(criterion.property)

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

between('total', 0L, 5L) on a relationship entity throws. The comparison uses total instead of r.total. Keep an ordinary relationship property qualified with its relationship variable.

…nship, whatever the owner has under the same name

A property name in the block of an association to a relationship entity was looked up on the entity that owns
the relationship, so a comparison on a name that is an association of the owner (Owner.address against
Link.address, a string) matched a node and the query threw. The scope of a relationship block is now the
relationship entity itself, with the nodes at its ends recorded for the from and to blocks inside it, and
CriterionScope.forCriterion, which only worked around the wrong scope, is gone.

RelationshipPersistentEntity.formatProperty treated every name that starts with 'from' or 'to' as an end node,
so a relationship property like 'total' was never qualified. It now matches the end itself or something of it.

RelationshipBlockMatrixSpec runs 12 criteria on three properties (an association of the owner, a property of a
different type, a property only the relationship has) from the root of a query and from a nested block, plus the
from and to blocks inside a relationship block.

Claude-Session: https://claude.ai/code/session_01Dmki4RzWwreEC2GkkyFnvL
@borinquenkid

Copy link
Copy Markdown
Member Author

@jamesfredley reproduced and fixed in 12e1fcc.

Cause. A block on an association to a relationship entity kept the entity that owns the relationship as its scope. With owner { links { eq('address', 'set') } }, reference() looked address up on Owner, found an association, and matched a node for a variable that is a relationship, so the query threw. My earlier CriterionScope change had the same flaw.

Fix. The scope of a relationship block is now the relationship entity itself. The nodes at its two ends are recorded on the scope, so a from { } or to { } block inside it resolves to the right node. CriterionScope.forCriterion only worked around the wrong scope and is removed.

A second bug the same cells exposed. RelationshipPersistentEntity.formatProperty treated every name that starts with from or to as an end node, so a relationship property like total or fromDate was never qualified with its variable. It now matches the end itself or something of it (to.name).

Matrix, not a single case. RelationshipBlockMatrixSpec runs 12 criteria (eq, ne, gt, ge, lt, le, like, ilike, in, between, isNull, isNotNull) on three properties (an association of the owner, a property of another type, a property only the relationship has), from the root of a query and from a nested block, plus from and to blocks inside a relationship block: 105 cells. Before the change, 28 failed: address under eq, ne, gt, ge, lt, le and in, from both entry points. All 105 pass now.

Neo4j core suite: 753 tests, 0 failures.

@jamesfredley jamesfredley left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Not approving. A disjunction that also tests membership of an association still drops the rows that match the other branch. A self-referential relationship also filters the wrong end.

return associated.formatId(association.name)
}
String target = "m_${builder.getNextMatchNumber()}"
builder.addMatch(entity.formatAssociationPatternFromExisting(association, '', variable, target))

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

or { eq('name', 'Other'); inList('parent', [other.id]) }, with Other having no parent. The list returns only the child. Other is dropped before the name test can keep it. Do not require the association for the whole query.

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fixed in 6bb0d55. Inside an or, an association reference no longer adds a required MATCH. A to-one reference is read in place with head([pattern | id]), and an association block is an existence test, size([pattern WHERE conditions | 1]) > 0. A node without the association is a null the branch does not match, so the other branches can still keep it. (The embedded server's driver rejects EXISTS { }, hence the pattern comprehensions.) Your or { eq('name','Other'); inList('parent', [other.id]) } case is in RelationshipBlockMatrixSpec, with the other criteria in or (block, eq, in, isNull, isNotNull).

Known limit: a to-many reference compared inside or still uses the required match, because a comprehension can read only one node. Blocks and null tests on to-many associations are fine.

The same behavior is documented for Hibernate ("accounts that have either performed transactions OR were recently created"), and the same bug on Hibernate 5 and 7 is in #16924, with fixes in #16930 and #16931.

RelationshipPersistentEntity relationshipEntity = (RelationshipPersistentEntity) block.entity
builder.addRelationshipVariable(variable, relationshipEntity)
// the node that owns the block is the end its entity is the relationship's, unless it is the other way round
boolean incoming = entity.is(relationshipEntity.to.associatedEntity) && !entity.is(relationshipEntity.from.associatedEntity)

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

links { from { eq('name', 'Alice') } } on a relationship whose ends are the same type filters the receiving node. Alice as the origin is missing. Use the direction of the association.

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fixed in 6bb0d55. matchForRelationshipEntity treated the owner as the receiving end whenever the owner was the to type, even when it was also the from type, while CriterionScope.into computed the opposite. For a relationship between the same type they disagreed. The owner is now the receiving end only when it is to and not from, so a self-referential relationship is outgoing from from, like alice.knows. links { from { eq('name', 'Alice') } } now filters the origin. The matrix has self-referential from and to block cells.

Two existing IsNullAssociationSpec expectations encoded the old backwards direction (the bond test, and isNull/isNotNull('feuds')). I changed them to the direction of the association, and the new owners in the bond test are explicit (with-set, with-unset). Please check those two.

… and enter a self-referential relationship from its origin

An association block, or a reference to a to-one association, added a required
match to the query, so inside or { } a node without the association was dropped
before the other branch could keep it. In a disjunction the block is now an
existence test over a pattern comprehension and a to-one reference reads the
node where it stands, so nothing narrows the rows the other branches see.

For a relationship between two nodes of one type the owner was taken as the
receiving end, so a block entered the relationship backwards and from/to named
the wrong node. The owner is at the receiving end only when it is not also the
origin, as navigating the association of an instance does.

RelationshipBlockMatrixSpec gains a combinator axis, an owner without the
association, and a self-referential relationship.

Claude-Session: https://claude.ai/code/session_01Dmki4RzWwreEC2GkkyFnvL
borinquenkid added a commit that referenced this pull request Nov 11, 2026
…ociated entity

A null test in the block matched the entities without the association, because the
left join gives null for the missing one. The block now also requires the row of the
join on a left join. The shared block cases skip Neo4j until #16836 is merged.

Claude-Session: https://claude.ai/code/session_01Dmki4RzWwreEC2GkkyFnvL
borinquenkid added a commit that referenced this pull request Nov 11, 2026
…ociated entity

A null test in the block matched the entities without the association, because the
left join gives null for the missing one. The block now also requires the id of the
joined alias. The shared block cases skip Neo4j until #16836 is merged.

Claude-Session: https://claude.ai/code/session_01Dmki4RzWwreEC2GkkyFnvL
@borinquenkid
borinquenkid requested a review from matrei November 12, 2026 14:13

@jamesfredley jamesfredley left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Not approving. A disjunction that tests membership of a collection still drops the rows that match the other branch.

if (entity.isRelationshipEntity() && RelationshipPersistentEntity.isRelationshipAssociation(association)) {
return associated.formatId(association.name)
}
if (builder.inDisjunction && found instanceof ToOne) {

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

or { eq('name', 's2'); inList('items', [i1.id]) }, where s2 has no items. The list returns only the shelf that has i1. s2 is dropped before the name test can keep it. Do not require the collection for the whole query.

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fixed in 9de206c. The disjunction case was one cell of a class: a criterion on a collection (inList, eq, the comparisons) took a required match of the collection, in any or or not. It is now read in place and holds when some element satisfies it, so s2 reaches the name branch. isEmpty and isNotEmpty on an association had the same family of problem (they tested a node property that does not exist) and now test the relationship, like isNull.

CollectionAssociationMatrixSpec covers 10 criteria in 6 contexts (top level, either side of an or, an and in an or, an or in an and, not), from the root and from a nested block: 120 cases, and 28 of the first 36 failed before the change. The whole Neo4j core suite passes (906 tests, 0 failures).

…fies it, in any context

In a disjunction or a negation a criterion on a collection of nodes took a
required match of the collection, which dropped the nodes that have none
before the other branches could keep them. The collection is now read where
it stands as the identities of its elements, so inList, eq and the
comparison criteria hold when some element satisfies them. isEmpty and
isNotEmpty on an association test the relationship, as isNull and
isNotNull already do, instead of the length of a node property that does
not exist.

CollectionAssociationMatrixSpec covers 10 criteria in 6 contexts (top level,
either side of a disjunction, a conjunction in a disjunction, a disjunction
in a conjunction, a negation) from the root and from a nested block, with
the expected rows worked out from the fixture by one oracle.

Claude-Session: https://claude.ai/code/session_01Dmki4RzWwreEC2GkkyFnvL

@jamesfredley jamesfredley left a comment •

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Requested changes. isNull and isNotNull on an association still test the relationship. A criterion on a collection fails when the only element satisfies it twice.

String pattern = entity.formatAssociationPatternFromExisting(association, '', variable, local)
return "head([${pattern} | ${associated.formatId(local)}])".toString()
}
String target = "m_${builder.getNextMatchNumber()}"

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Shelf s1 has one item i1. inList('items', [i1.id]) twice should return s1. It returns nothing. Each criterion adds another relationship pattern, and those patterns are combined into one match that cannot reuse the only relationship. A collection criterion should hold when some element satisfies it, including when the same element satisfies two of them.

…fies it, in any context

Answers the review of #16836: two criteria that one element satisfies found nothing outside a disjunction, because each added a relationship pattern to one MATCH and Cypher does not let two patterns share a relationship. Every collection criterion now reads its members where the collection stands; the grid gains a pairs dimension.

Claude-Session: https://claude.ai/code/session_01Dmki4RzWwreEC2GkkyFnvL
@borinquenkid

Copy link
Copy Markdown
Member Author

Answers the second sentence of the review: two criteria that one element satisfies. A criterion on a collection now always reads its members where the collection stands, so two criteria no longer need two relationships in one MATCH, which Cypher does not let two patterns share.

CollectionAssociationMatrixSpec gains a pairs dimension: every criterion with every other, the expected shelves from one oracle (the intersection of what each matches alone); 12 of its 220 rows fail without the change. Neo4j core: 1030 tests, 0 failures; codeStyle clean.

Not addressed in this commit: the first sentence of the review (isNull and isNotNull on an association). I have not re-checked it against the current head.

@jamesfredley jamesfredley left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Requested changes. A criterion on a collection holds when the only element satisfies it twice. A criterion on a relationship endpoint still names a variable that was never bound.

if (found instanceof Association && !(found instanceof Basic)) {
Association association = (Association) found
GraphPersistentEntity associated = (GraphPersistentEntity) association.associatedEntity
if (entity.isRelationshipEntity() && RelationshipPersistentEntity.isRelationshipAssociation(association)) {

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

links { eq('to', target) } should match the links that end at that target. The query names to, and that name was never bound, so the query fails. Use the variable the block already bound for that end.

…he node the block bound for it

eq('to', target) in a block on a relationship named the end by the property name, a Cypher variable the query never bound, so
the query failed with Variable `to` not defined. The end is now the variable the block bound for that end (the node the
block was entered from, or the one it reaches), and the pattern's own name when the relationship is the root of the query.

RelationshipBlockIdentitySpec gains rows for to and from, equal and not equal, beside another criterion, in a disjunction,
and with the relationship as the root of the query.

Claude-Session: https://claude.ai/code/session_01Dmki4RzWwreEC2GkkyFnvL
@borinquenkid

Copy link
Copy Markdown
Member Author

Thank you, you are right: an end of a relationship in a block was named by the property name, to or from, a Cypher variable the query never bound, so links { eq('to', target) } failed with Variable to not defined.

The end is now the variable the block bound for it: the node the block was entered from, or the node it reaches, as forEnd already does for a nested block. When the relationship is the root of the query, the pattern's own name for the end is used, as before.

RelationshipBlockIdentitySpec has rows for the two ends, equal and not equal: to with the node the link reaches (found) and another node (nothing), from with the owner, ne both ways, beside another criterion in the block, in a disjunction with a property, and with the relationship as the root of the query. They fail without the fix (2 of 8, the others being the existing rows) and pass with it. The Neo4j module is green (1033 tests) and codeStyle is clean.

This branch is the base of #17011, which will take it on its next update.

@jamesfredley jamesfredley left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Requested changes. An outgoing relationship end is compared through the node the block bound. A type comparison and a null test on that end still name a variable that was never bound.

// the node reached is of the associated entity, whose identity may be mapped differently
return associated.formatId(target)
}
if (entity.isRelationshipEntity() && name == RelationshipPersistentEntity.TYPE) {

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

links { eq('type', 'COLLINK') } should match the links of that type. The query names r, and the block bound the relationship as another variable, so the owner is dropped. Use the variable the block already bound.

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

Status: No status

Development

Successfully merging this pull request may close these issues.

Neo4j: isNull on an association matches every node Checking Null on Relationship/nested Node using Criteria doesn't generate the correct CYPHER query

4 participants